package com.hzsk.lisence.controller;

import com.hzsk.lisence.entity.SoftwareSonEntity;
import com.hzsk.lisence.entity.query.SelectByNameQuery;
import com.hzsk.lisence.entity.response.IncResponce;
import com.hzsk.lisence.service.SoftwareSonService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/lisence/softwareson/")
public class SoftwareSonController {

    @Resource
    public SoftwareSonService softwareSonService;
    @GetMapping("getsoftwarelistsoninfo")
    @ApiOperation(value = "根据软件id查询子模块列表")
    public IncResponce getSoftwareListSonInfo(@RequestParam("softwareId") int softwareId) {
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(softwareSonService.getSoftwareListSonInfo(softwareId));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }

    @PostMapping("getsoftwaresonlistselected")
    @ApiOperation(value = "搜索软件子模块列表")
    public IncResponce getSoftwareListSelected(@RequestBody SelectByNameQuery selectByNameQuery)
    {
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(softwareSonService.getSoftwareSonListSelected(selectByNameQuery));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }

    @PostMapping("savesoftwaresoninfo")
    @ApiOperation(value = "保存软件子模块信息")
    public IncResponce saveSoftwareSonInfo(@RequestBody SoftwareSonEntity softwareSonEntity) {
        IncResponce incResponce = new IncResponce();
        try {
            synchronized (this){
                softwareSonService.saveSoftwareSonInfo(softwareSonEntity,softwareSonEntity.getOperatorUserId(),incResponce);
            }
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @PutMapping("updatesoftwaresoninfo")
    @ApiOperation(value = "修改软件子模块信息")
    public IncResponce updateSoftwareSonInfo(@RequestBody SoftwareSonEntity softwareSonEntity) {
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            softwareSonService.updateSoftwareSonInfo(softwareSonEntity,softwareSonEntity.getOperatorUserId(),incResponce);
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }



    @DeleteMapping("delsoftwaresoninfo")
    @ApiOperation(value = "删除软件子模块")
    public IncResponce delSoftwareSonInfo(@RequestParam int[] ids, @RequestParam Integer operatorUserId) {
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(softwareSonService.delSoftwareSonInfo(ids,operatorUserId));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


}
